package com.example.biji.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.biji.dao.UserDao;
import com.example.biji.model.User;
import com.example.biji.R;

public class LoginActivity extends AppCompatActivity {
    private EditText etUsername, etPassword;
    private CheckBox cbRemember;
    private Button btnLogin;
    private TextView tvRegister; // 添加对注册文本的引用
    private UserDao userDao;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initViews();
        loadSavedCredentials();
        setupListeners();
    }

    private void initViews() {
        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        cbRemember = findViewById(R.id.cb_remember);
        btnLogin = findViewById(R.id.btn_login);
        tvRegister = findViewById(R.id.tv_register); // 初始化注册文本
        userDao = new UserDao(this);
        sharedPreferences = getSharedPreferences("login_prefs", MODE_PRIVATE);
    }

    private void loadSavedCredentials() {
        boolean remember = sharedPreferences.getBoolean("remember", false);
        if (remember) {
            String username = sharedPreferences.getString("username", "");
            String password = sharedPreferences.getString("password", "");
            etUsername.setText(username);
            etPassword.setText(password);
            cbRemember.setChecked(true);
        }
    }

    private void setupListeners() {
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });

        // 添加注册文本的点击事件处理
        tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateToRegister();
            }
        });
    }

    private void login() {
        String username = etUsername.getText().toString().trim();
        String password = etPassword.getText().toString().trim();

        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        User user = userDao.queryUser(username);
        if (user == null) {
            Toast.makeText(this, "用户不存在", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!password.equals(user.getPassword())) {
            Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show();
            return;
        }

        saveLoginCredentials(username, password, cbRemember.isChecked());

        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("userId", user.getId());
        intent.putExtra("username", username);
        startActivity(intent);
        finish();
    }

    private void saveLoginCredentials(String username, String password, boolean remember) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("remember", remember);
        if (remember) {
            editor.putString("username", username);
            editor.putString("password", password);
        } else {
            editor.remove("username");
            editor.remove("password");
        }
        editor.apply();
    }

    // 添加跳转到注册页面的方法
    private void navigateToRegister() {
        Intent intent = new Intent(this, RegisterActivity.class);
        startActivity(intent);
    }
}